#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

#define DEBUG 0
#define NOISY_TEST 1

int checkAdventurer(int currPlayer, int handPos, struct gameState *post)
{
	int r, hand;
	hand = post->handCount[currPlayer];

	r = playCard(handPos, -1, -1, -1, post);
	if (r != 0) {
		printf("Failed to play card.\n");
		return r;
	}
	if (post->handCount[currPlayer] != hand + 2) {
		printf("Hand count mismatch. Expected:%d  Result:%d\n",
		       hand+2, post->handCount[currPlayer]);
		r--;
	}
	
	int a = post->hand[currPlayer][post->handCount[currPlayer]-2];
	int b = post->hand[currPlayer][post->handCount[currPlayer]-1];
	
	if (a != copper && a != silver && a != gold) {
		printf("1st new card is not treasure. Actual:%d\n", a);
		r--;
	}
	
	if (b != copper && b != silver && b != gold) {
		printf("2nd new card is not treasure. Actual:%d\n", b);
		r--;
	}
	
	return r;
}

int main()
{
	int i, n, p, handPos;
	int k[10] = {adventurer, council_room, feast, gardens, mine,
	             remodel, smithy, village, baron, great_hall};
	
	struct gameState G;
	printf("ADVENTURER RANDOM TESTS BEGIN:.\n");
	
	SelectStream(2);
	PutSeed(3);
	
	for(n = 0; n < 2000; n++) {
		for(i = 0; i < sizeof(struct gameState); i++) {
			((char*)&G)[i] = floor(Random() * 256);
		}
		//p = floor(Random() * MAX_PLAYERS);
		p = 4;

		int r = initializeGame(p, k, 1, &G);
		if (r != 0)
			printf("Failed to initialize game state!\n");
		
		int currPlayer = floor(Random() * p);
		G.whoseTurn = currPlayer;
		G.deckCount[currPlayer] = floor(Random() * MAX_DECK);
		G.discardCount[currPlayer] = floor(Random() * MAX_DECK);
		G.handCount[currPlayer] = floor(Random() * MAX_HAND);
		
		//Fill hand, deck, and discard with random cards
		for (i = 0; i < G.handCount[currPlayer]; i++) {
			int card = floor(Random() * 17);
			if (card < 7)
				G.hand[currPlayer][i] = card;
			else
				G.hand[currPlayer][i] = k[card - 7];
		}
		for (i = 0; i < G.deckCount[currPlayer]; i++) {
			int card = floor(Random() * 17);
			if (card < 7)
				G.deck[currPlayer][i] = card;
			else
				G.deck[currPlayer][i] = k[card - 7];
		}		
		for (i = 0; i < G.discardCount[currPlayer]; i++) {
			int card = floor(Random() * 17);
			if (card < 7)
				G.discard[currPlayer][i] = card;
			else
				G.discard[currPlayer][i] = k[card - 7];
		}

		handPos = floor(Random() * G.handCount[currPlayer]);
		G.hand[currPlayer][handPos] = adventurer;

		checkAdventurer(currPlayer, handPos, &G);
	}
	
	printf("ALL RANDOM TESTS PASSED.\n");
	return 0;
}
